package cn.java79.quiz.service;

import cn.java79.quiz.dao.QuestionsMapper;
import cn.java79.quiz.entity.Questions;
import cn.java79.quiz.pojo.AjaxPage;
import cn.java79.quiz.util.MybatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

/**
 * @author oldliu
 * @since 1.0
 */
public class QuestionsService {
    public static final int PAGESIZE=20;
    public static QuestionsService ME=new QuestionsService();
    public AjaxPage<Questions> search(String title, String sectionName,int pageNo,int pageSize){

        SqlSession s = MybatisUtil.get();
        Page<Questions> page = PageHelper.startPage(pageNo, pageSize);
        try {

            QuestionsMapper mapper = s.getMapper(QuestionsMapper.class);
            mapper.search(title,sectionName);
            return new AjaxPage<Questions>(page.subList(0, page.size()), page.getTotal(), page.getPageNum(), page.getPageSize(), page.getPages());
        } finally {
            MybatisUtil.close(s);
        }

    }

    public   int delete(Long id){
        SqlSession s = MybatisUtil.get();

        try {
            QuestionsMapper mapper = s.getMapper(QuestionsMapper.class);
            return mapper.delete(id);
        } finally {
            MybatisUtil.close(s);
        }
    }


    public int insert(Questions record){
        SqlSession s = MybatisUtil.get();
        try {
            QuestionsMapper mapper = s.getMapper(QuestionsMapper.class);
            return mapper.insert(record);
        } finally {
            MybatisUtil.close(s);
        }
    }



    public Questions get(Long id){
        SqlSession s = MybatisUtil.get();
        try {
            QuestionsMapper mapper = s.getMapper(QuestionsMapper.class);
            return mapper.get(id);
        } finally {
            MybatisUtil.close(s);
        }
    }

    public int update(Questions record){
        SqlSession s = MybatisUtil.get();
        try {
            QuestionsMapper mapper = s.getMapper(QuestionsMapper.class);
            return mapper.update(record);
        } finally {
            MybatisUtil.close(s);
        }
    }

    //随机选题
    public Questions randomSel( long skillid, long[]qids){
        SqlSession s = MybatisUtil.get();
        try {
            QuestionsMapper mapper = s.getMapper(QuestionsMapper.class);
            return mapper.randomSel(skillid, qids);
        } finally {
            MybatisUtil.close(s);
        }
    }


}
